<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./bootstrap.min.css">
</head>
<body>
  <script src="./vue.js"></script>
  <div id="app">
    <table class="table table-bordered table-hover table-striped">
      <th>添加品牌</th>
    </table>
    <tbody>
      <td>
       <form @submit.prevent="add">
        <span>品牌名称</span>
        <input type="text" placeholder="请输入姓名" v-model.trim="brand">
        <button type="submit">添加</button>
       </form>
      </td>
    </tbody>
    <table class="table table-bordered table-hover table-striped">
      <thead>
        <th>索引</th>
        <th>Id</th>
        <th>姓名</th>
        <th>状态</th>
        <th>操作</th>
        <th>时间</th>
      </thead>
      <tbody>
        <!-- 官方建议，只要使用了v-for指令，那么一定要绑定一个:key属性 -->
        <!-- 而且，尽量把id作为key的值 -->
        <tr v-for="(item,index) in list" :key="item.id"   :title="item.name">
          <td>{{index}}</td>
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>
            <input v-model="item.status" type="checkbox" :id="item.id">
            <label v-if="item.status" :for="item.id">已启用</label>
            <label v-else :for="item.id">已禁用</label>
            <input type="text" v-model=item.status>
          </td>
          <td><a  @click="remove(item.id)" href="javascript:;" style="text-decoration: none;">{{item.cz}}</a></td>
          <td>{{item.time}}</td>
        </tr>
      </tbody>
    </table>
  </div>
  <script>
  const vm = new Vue({
    el:'#app',
    data:{
      tips:'123',
      // 用户输入的查找姓名
      brand:'',
      //下一个可用的id
      nextId:5,
      list:[
        {id:1,name:'张三',status:true,time: new Date(),cz:'删除'},
        {id:2,name:'李四',status:'',time: new Date(),cz:'删除'},
        {id:3,name:'王五',status:'',time: new Date(),cz:'删除'},
        {id:4,name:'老六',status:'',time: new Date(),cz:'删除'}
      ],

    },
    methods:{
      remove(id) {
        console.log(id);
        //点击链接删除对应的品牌
        // filter是数组里面的方法，用于过滤
        this.list =  this.list.filter(item => item.id != id)
      },
      add() {
        if (this.brand == '') {
          alert('姓名不能为空')
          return
        }
        console.log(this.brand);
        const obj = {
          id : this.nextId,
          name:this.brand,
          status:true,
          cz:'删除',
          time: new Date()
        }
        this.list.push(obj);
        this.brand = '';
        this.nextId++;
      }
    }
  })
  </script>
</body>
</html>